package com.tau.egonetwork.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class VisualizationPreferencesFile {

	public static final String DISPLAY_STATISTICS_HOVER = "Hover on vertex";
	public static final String DISPLAY_STATISTICS_CLICK = "Click on vertex";

	public static final String SHOW_NON_CLUSTER_EDGES_FADED = "Faded";
	public static final String SHOW_NON_CLUSTER_EDGES_DONT_SHOW = "Don't show at all";

	private static final String CONFIG = "visualization.properties";
	
	private static final String CONFIG_ALERT = "alertWhenUnder4";
	private static final String CONFIG_DISPLAY_STATISTICS = "displayStatistics";
	private static final String CONFIG_SHOW_VERTICES = "showVerticesNotInAnyCluster";
	private static final String CONFIG_SHOW_BOLD_EDGES = "showClusterEdgesAsBold";
	private static final String CONFIG_SHOW_EDGES_NOT_IN_CLUSTER = "showEdgesNotInCluster";

	private static boolean alertMeWhenUnder4 = false;
	private static String displayStatistics = null;
	private static boolean showVerticesNotInAnyCluster = false;
	private static boolean showClusterEdgesAsBold = false;
	private static String showEdgesNotInCluster = null;
	
	private static boolean preferencesFileExists = false;
	
	static {
		
		Properties properties = new Properties();
		try {
			
			properties.load(new FileInputStream(CONFIG));
			preferencesFileExists = true;
			
			alertMeWhenUnder4 = 
					Boolean.parseBoolean(properties.getProperty(CONFIG_ALERT));

			displayStatistics = 
					properties.getProperty(CONFIG_DISPLAY_STATISTICS);

			showVerticesNotInAnyCluster = 
					Boolean.parseBoolean(properties.getProperty(CONFIG_SHOW_VERTICES));

			showClusterEdgesAsBold = 
					Boolean.parseBoolean(properties.getProperty(CONFIG_SHOW_BOLD_EDGES));

			showEdgesNotInCluster = 
					properties.getProperty(CONFIG_SHOW_EDGES_NOT_IN_CLUSTER);

		} catch (FileNotFoundException e) {
		} catch(IOException e) { }
	}
	
	public static void savePreferences(boolean alertMe, String displayStats,
			boolean showVertices, boolean showClusterEdges, String showNonClusterEdges) {
				
		Properties properties = new Properties();
		properties.setProperty(CONFIG_ALERT, alertMe + "");
		properties.setProperty(CONFIG_DISPLAY_STATISTICS, displayStats);
		properties.setProperty(CONFIG_SHOW_VERTICES, showVertices + "");
		properties.setProperty(CONFIG_SHOW_BOLD_EDGES, showClusterEdges + "");
		properties.setProperty(CONFIG_SHOW_EDGES_NOT_IN_CLUSTER, showNonClusterEdges);
		
		try {
			FileOutputStream fos = new FileOutputStream(new File(CONFIG));
			properties.store(fos, null);
		} catch (FileNotFoundException e) {
			Log.writeLineToLog("VisualizationPreferencesFile.savePreferences()");
			Log.writeExceptionToLog(e);
		} catch (IOException e) {
			Log.writeLineToLog("VisualizationPreferencesFile.savePreferences()");
			Log.writeExceptionToLog(e);
		} finally {
			
			preferencesFileExists = true;
			alertMeWhenUnder4 = alertMe;
			displayStatistics = displayStats;
			showVerticesNotInAnyCluster = showVertices;
			showClusterEdgesAsBold = showClusterEdges;
			showEdgesNotInCluster = showNonClusterEdges;
		}
		
	}

	public static boolean isVisualizationPreferencesFileExists() {
		return preferencesFileExists;
	}

	public static boolean getAlertMeWhenUnder4() {
		return alertMeWhenUnder4;
	}

	public static String getDisplayStatistics() {
		return displayStatistics;
	}

	public static boolean getShowVerticesNotInAnyCluster() {
		return showVerticesNotInAnyCluster;
	}

	public static boolean getShowClusterEdgesAsBold() {
		return showClusterEdgesAsBold;
	}

	public static String getShowEdgesNotInCluster() {
		return showEdgesNotInCluster;
	}	
	
}
